﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace PyramidShadow
{
    class Sign : LevelObject
    {
        private SpawnPoint spawnPoint;
        public SpawnPoint SpawnPoint
        {
            get { return spawnPoint; }
        }
        public Rectangle ReadingArea
        {
            get { return readingArea; }
        }

        private bool displayText;
        private bool read;
        private bool hasFunction = false;
        private PS_Event ps_event;
        private TextField textField;
        private Rectangle readingArea;
        private const byte readingOffset = 15;

        public Sign(Texture2D texture, Vector2 position, Level level, PS_Event ps_event, TextField textField)
            : this(texture, position, level, textField)
        {
            hasFunction = true;
            this.ps_event = ps_event;
        }

        public Sign(Texture2D texture, Vector2 position, Level level, TextField textField) : base(texture, position, level)
        {
            this.textField = textField;
            spawnPoint = new SpawnPoint(position);
            read = false;
            displayText = false;
            readingArea = new Rectangle(boundingBox.X - readingOffset, boundingBox.Y - readingOffset, boundingBox.Width + (2 * readingOffset), boundingBox.Height + (2 * readingOffset));
        }



        public override void Update(GameTime gameTime, KeyboardState currentKeyboardState)
        {
            if (displayText)
            {
                while (displayText)
                {
                    currentKeyboardState = Keyboard.GetState();
                    if(currentKeyboardState.IsKeyDown(Keys.Escape))
                        displayText = false;
                }
            }
            //Chekcs for the ghost reading
            if (level.kollision.intersection(level.ghost.BoundingBox, readingArea)&&currentKeyboardState.IsKeyDown(Keys.Enter))
            {
                displayText = true;
                if (!read && hasFunction)
                {
                    read = true;
                    ps_event.doIt();
                }
            }
            //checks for the body colliding
            if (level.kollision.intersection(level.body.boundingBox, boundingBox))
                level.body.jumpBack();
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, boundingBox, Color.White);

            if (displayText)
            {
                textField.draw(spriteBatch);
            }
        }
    }
}
